package cn.shiro.shiroservice.moudle.db.controller.other;

import cn.shiro.shiroservice.common.utils.R;
import cn.shiro.shiroservice.common.utils.SqlSessionUtils;
import cn.shiro.shiroservice.moudle.db.abstracts.other.AbstractCoreDatabaseController;
import cn.shiro.shiroservice.moudle.db.mapper.DataBaseMapper;
import cn.shiro.shiroservice.moudle.db.pojo.dto.TableStructureDTO;
import cn.shiro.shiroservice.moudle.db.service.DatabaseService;
import cn.shiro.shiroservice.moudle.db.service.impl.DatabaseServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * &#064;Time 2024 一月 星期一 14:02
 *
 * @author ShangGuan
 */
@RestController
@RequestMapping("/database")
public  class DatabaseController extends AbstractCoreDatabaseController {

    private DatabaseService databaseService;

    @GetMapping("/getVersion")
    @RequiresAuthentication
    public R<String> getDatabaseVersion(){
        loadDataSource();
        try (SqlSession session = SqlSessionUtils.getSqlSession(cacheMapDataSource.get(getName(getUsername())),DataBaseMapper.class)){
            databaseService= new DatabaseServiceImpl(session.getMapper(DataBaseMapper.class));
            return R.ok(databaseService.getDataBaseVersion());
        }
    }

    @GetMapping("/showDatabases")
    @RequiresAuthentication
    public R<List<String>> showDatabase(){
        loadDataSource();
        try (SqlSession session = SqlSessionUtils.getSqlSession(cacheMapDataSource.get(getName(getUsername())),DataBaseMapper.class)){
            databaseService= new DatabaseServiceImpl(session.getMapper(DataBaseMapper.class));
            return R.ok(databaseService.showDatabase());
        }
    }
    @GetMapping("/showTables")
    @RequiresAuthentication
    public R<List<String>> showTables(String databaseName){
        loadDataSource(databaseName);
        try (SqlSession session = SqlSessionUtils.getSqlSession(cacheMapDataSource.get(getName(getUsername())),DataBaseMapper.class)){
            databaseService= new DatabaseServiceImpl(session.getMapper(DataBaseMapper.class));
            return R.ok(databaseService.showTables());
        }
    }

    @GetMapping("/getTableStructure")
    @RequiresAuthentication
    public R<List<TableStructureDTO>> getTableStructure(String databaseName, String tableName){
        loadDataSource(databaseName);
        try (SqlSession session = SqlSessionUtils.getSqlSession(cacheMapDataSource.get(getName(getUsername())),DataBaseMapper.class)){
            databaseService= new DatabaseServiceImpl(session.getMapper(DataBaseMapper.class));
            return R.ok(databaseService.getTableStructure(tableName));
        }
    }

    /**
     * 查询表格内容
     *
     * @param databaseName 数据库名称
     * @param tableName    表名称
     * @return {@link R}<{@link List}<{@link Object}>>
     */

    @GetMapping("/selectTable")
    @RequiresAuthentication
    public R<List<Map<String,Object>>> selectTable(String databaseName, String tableName){
        loadDataSource(databaseName);
        try (SqlSession session = SqlSessionUtils.getSqlSession(cacheMapDataSource.get(getName(getUsername())),DataBaseMapper.class)){
            databaseService= new DatabaseServiceImpl(session.getMapper(DataBaseMapper.class));
            return R.ok(databaseService.selectTable(tableName));
        }
    }
    @GetMapping("/page")
    @RequiresAuthentication
    public R<PageInfo<?>> page(String databaseName, String tableName, Integer pageNum, Integer pageSize){
        loadDataSource(databaseName);
        try (SqlSession session = SqlSessionUtils.getSqlSession(cacheMapDataSource.get(getName(getUsername())),DataBaseMapper.class)){
            databaseService= new DatabaseServiceImpl(session.getMapper(DataBaseMapper.class));
            return R.ok(databaseService.page(tableName,pageNum,pageSize));
        }
    }
}
